package com.pingtai.indent.dao;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import com.pingtai.dao.BaseDao;
import com.pingtai.indent.entity.Qq;
import com.pingtai.indent.entity.RenzhengMid;
import com.pingtai.indent.entity.TencentBusinessRenzheng;
import com.pingtai.indent.entity.TencentPersonRenzheng;
import com.pingtai.indent.entity.UserBaseIndentType;
import com.pingtai.indent.entity.UserIndent;
import com.pingtai.indent.entity.UserIndentMoneyTmp;
import com.pingtai.indent.entity.UserIndentType;
import com.pingtai.indent.entity.XinlangPersonRenzheng;
import com.pingtai.user.entity.User;
import com.pingtai.user.entity.UserPay;

public class IndentDao extends BaseDao {

	public List getUserConsumptionTotalMoney(User user){
		Session session = getSession();
		Query query = session.createQuery("from UserIndentMoneyTmp  where user=:user");
		query.setParameter("user", user);
		return query.list();
	}
	
	public  UserIndentMoneyTmp getIndentMoneyTmpByIndentId(Integer indentId){
		Session session = getSession();
		Query query = session.createQuery("from UserIndentMoneyTmp  where userIndent.id=:indentId");
		query.setParameter("indentId", indentId);
		List l = query.list();
		if(l!=null&&l.size()>0){
			return (UserIndentMoneyTmp) l.get(0);
		}
		return null;
	}
	
	public Integer getWeixinSanmeNum(String weixinNum) throws ParseException{
		String sql = "select count(*) from Qq where weixin=:weixin";
		Session session = getSession();
		Query query = session.createQuery(sql);
		query.setParameter("weixin", weixinNum);
		Long result = (Long) query.uniqueResult(); 
		Integer count = result.intValue();
		
		if(count>0){
			sql="from Qq where weixin=:weixin";
			query = session.createQuery(sql);
			query.setParameter("weixin", weixinNum);
			List list = query.list();
			SimpleDateFormat bartDateFormat = 
					new SimpleDateFormat("yyyy-MM-dd");
			String dateStringToParse = "2013-11-17";
			Date date = bartDateFormat.parse(dateStringToParse);
			for(int i=0;i<list.size();i++){
				Qq qq = (Qq) list.get(i);
				UserIndent indent = qq.getUserIndent();
				if(indent.getState()==3){
					count--;
				}else{
					Date d = indent.getCreateDate();
					if(d.getTime()<date.getTime()){
						count--;
					}
				}
				
				
			}
		}
		return count;
	}
	
	public List findUserIndentTypeByUserAndType(User user,int id){
		Session session = getSession();
		Query query = session.createQuery("from UserIndentType  where user=? and userBaseIndentType.id=?");
		query.setParameter(0, user);
		query.setParameter(1, id);
		return query.list();
	}
	
	public List getIndentcrement(Date start ,Date end,UserIndent indent){
		Session session = getSession();
		Query query = session.createQuery("from IndentNumberTmp  where userIndent.id=:indentId and date between :start and :end ");
		query.setParameter("indentId", indent.getId());
		query.setParameter("start", start);
		query.setParameter("end", end);
		return query.list();
	}
	
	public void deleteAllIndentByBase(List baseList,Integer state,Integer day){
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd"); 
		Calendar calendar = Calendar.getInstance();  
		calendar.setTime(date);   
		calendar.add(Calendar.DATE, -1*day);
		Date newDate = calendar.getTime();
		for(int i=0;i<baseList.size();i++){
			Integer baseType =  (Integer) baseList.get(i);
			String sql = "from  UserIndent where baseIndentType.id=:baseId and state=:state and ";
			
			
			if(state==5) {		//退款
				sql += "refundDate<=:date";
			}else if(state==6){	//完成
				sql += "endDate<=:date";
			}else if(state==2){	//完成
				sql += "endDate<=:date";
			}
			
			
			Session session = getSession();
			Query query = session.createQuery(sql);
			query.setParameter("baseId", baseType);
			query.setParameter("state", state);
			query.setParameter("date", newDate);
			
			List list = query.list();
			if(state==2){
				for(int j=0;j<list.size();j++){
					UserIndent indent = (UserIndent) list.get(j);
					session.delete(indent);
				}
			}else{
				for(int j=0;j<list.size();j++){
					UserIndent indent = (UserIndent) list.get(j);
					RenzhengMid mid = indent.getRenzheng();
					TencentBusinessRenzheng tb =  mid.getTencentBusinessRenzheng();
					TencentPersonRenzheng tp = mid.getTencentPersonRenzheng();
					XinlangPersonRenzheng xp = mid.getXinlangPersonRenzheng();
					
					if(tb!=null) session.delete(tb);
					if(tp!=null) session.delete(tp);
					if(xp!=null) session.delete(xp);
					session.delete(mid);
					session.delete(indent);
				}
			}
			
		}
	}
	
	public List getAllIndentByBase(UserBaseIndentType base,List list){
		String sql = "from UserIndent where baseIndentType.id=:baseId and ";
		if(list!=null){
			sql += "(";
			for(int i=0;i<list.size();i++){
				if(i!=0){
					sql += " or ";	
				}
				Integer state = (Integer) list.get(i);
				sql += " state=:state"+i;
			}
			sql += ")";
		}
		Session session = getSession();
		Query query = session.createQuery(sql);
		query.setParameter("baseId", base.getId());
		if(list!=null){
			for(int i=0;i<list.size();i++){
				Integer s = (Integer) list.get(i);
				query.setParameter("state"+i, s);
			}
		}
		return query.list();
	}
	
	public List getAllIndentOfCreateByDate(Date start,Date end,UserBaseIndentType baseIndentType){
		Session session = getSession();
		Query query = session.createQuery("from UserIndent as a   where a.createDate between :start and :end and a.baseIndentType.id=:baseId and a.state!=3 order by a.createDate desc");
		query.setParameter("start", start);
		query.setParameter("end", end);
		query.setParameter("baseId", baseIndentType.getId());
		return query.list();
	}
	
	public List getAllIndentOfCompletedByDate(Date start,Date end,UserBaseIndentType baseIndentType){
		Session session = getSession();
		Query query = session.createQuery("from UserIndent as a   where a.endDate between :start and :end and a.baseIndentType.id=:baseId and a.state=2 order by a.endDate desc");
		query.setParameter("start", start);
		query.setParameter("end", end);
		query.setParameter("baseId", baseIndentType.getId());
		return query.list();
	}
	
	public Long getUserIndentNumberByStateAndBase(User user,Integer state,UserBaseIndentType base){
		Session session = getSession();
		Query query = session.createQuery("select count(*) from UserIndent u,UserIndentType t  where  u.state=? and u.baseIndentType.parentIdString like ? and t.user=? and u.baseIndentType=t.userBaseIndentType");
		query.setParameter(0, state);
		query.setParameter(1, "%:"+base.getId()+":%");
		query.setParameter(2, user);
		return ((Long)query.uniqueResult()).longValue();
	}
	
	public List findAllUserIndentTypeNoParent(User user){
		Session session = getSession();
		Query query = session.createQuery("from UserIndentType  where user=? and parentIndentType=null");
		query.setParameter(0, user);
		return query.list();
	}
	
	public List findAllChildBaseIndentType(UserBaseIndentType baseIndnetType){
		Session session = getSession();
		Query query = session.createQuery("from UserBaseIndentType  where parentBaseIndentType.id=?");
		query.setParameter(0, baseIndnetType.getId());
		return query.list();
	}
	
	public void updateIndentPay(){
		Session session = getSession();
		Query query = session.createQuery("from UserPay");
		List list = query.list();  
		for(int i=0;i<list.size();i++){
			UserPay userPay = (UserPay) list.get(i);
			UserIndent indent = userPay.getUserIndent();
			if(indent!=null){
				userPay.setUserBaseIndent(indent.getBaseIndentType());
				userPay.setIndentCreateUser(indent.getUser());
				session.update(userPay);
			}
			
		}
	}
	
	public List findAllRuningIndent(int base){
		Session session = getSession();
		Query query = session.createQuery("from UserIndent  where baseIndentType.id=? and state=?");
		query.setParameter(0, base);
		query.setParameter(1, 1);
		return query.list();
	}
	
	public List getAllUserIndentType(){
		Session session = getSession();
		Query query = session.createQuery("from UserIndentType");
		return query.list();
	}
	
	public List getAllBaseIndentType(){
		Session session = getSession();
		Query query = session.createQuery("from UserBaseIndentType  ");
		return query.list();
	}
	
	public List getAllIndentRebateRelation(User user ,int state){
		Session session = getSession();
		Query query = session.createQuery("from IndentRebateRelation  where user.userId=? and userIndent.state=? and rebateMoney!=0");
		query.setParameter(0, user.getUserId());
		query.setParameter(1,state);
		return query.list();
	}
	
	public List getAllIndentRebateRelation(User user ,int state,Date begin,Date end){
		Session session = getSession();
		Query query = session.createQuery("from IndentRebateRelation  where user=? and userIndent.state=? and rebateMoney!=0 and userIndent.endDate between ? and ?");
		query.setParameter(0, user);
		query.setParameter(1,state);
		query.setParameter(2,begin);
		query.setParameter(3,end);
		return query.list();
	}
	
	public Qq getQqByIndentId(Integer indentId){
		Session session = getSession();
		Query query = session.createQuery("from Qq  where userIndent.id=?");
		query.setParameter(0, indentId);
		List list = query.list();
		if(list!=null&&list.size()>0) return (Qq) list.get(0);
		return null;
	}
	
	public List getIndentNumberTmp(UserIndent userIndent){
		Session session = getSession();
		Query query = session.createQuery("from IndentNumberTmp  where userIndent = :userIndent order by id desc");
		query.setParameter("userIndent", userIndent);
		return query.list();
	}
	
	public List getQqPlzbTypeList(){
		Session session = getSession();
		Query query = session.createQuery("from IndentQqPlzbType  order by id asc");
		return query.list();
	}
	
	public List getXinlangZbTypeList(){
		Session session = getSession();
		Query query = session.createQuery("from IndentXinlangZbType  order by id asc");
		return query.list();
	}
}
